package com.zeng.sim.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.sim.controller.domain.SysDictData;
import com.zeng.sim.controller.domain.request.PageDomain;
import com.zeng.sim.controller.domain.response.Result;
import com.zeng.sim.controller.domain.response.ResultTable;
import com.zeng.sim.mapper.model.SysDictDataEntity;
import com.zeng.sim.service.SysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zengwanlin
 * @since 2023-12-20
 */
@Controller
@RequestMapping("/sys/dictData")
public class SysDictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;

    @GetMapping("/data")
    @ResponseBody
    public ResultTable data(SysDictData sysDictData, PageDomain pageDomain) {
        Page<SysDictData> page = sysDictDataService.pageQuery(sysDictData, pageDomain);
        return ResultTable.pageTable(page);
    }

    @GetMapping("/add")
    public String add(Model model, String typeCode) {
        model.addAttribute("typeCode", typeCode);
        return "system/dict/data/add";
    }

    @GetMapping("/edit")
    public String edit(Model model, Integer dataId) {
        SysDictDataEntity entity = sysDictDataService.getById(dataId);
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(entity, sysDictData);
        model.addAttribute("sysDictData", sysDictData);
        return "system/dict/data/edit";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody SysDictData sysDictData) {
        return Result.decide(sysDictDataService.saveSysDictData(sysDictData));
    }

    @PutMapping("/update")
    @ResponseBody
    public Result update(@RequestBody SysDictData sysDictData) {
        return Result.decide(sysDictDataService.updateSysDictData(sysDictData));
    }

    @DeleteMapping("/remove/{dataId}")
    @ResponseBody
    public Result remove(@PathVariable Integer dataId) {
        return Result.decide(sysDictDataService.removeById(dataId));
    }

    @DeleteMapping("/batchRemove/{dataIds}")
    @ResponseBody
    public Result batchRemove(@PathVariable String dataIds) {
        return Result.decide(sysDictDataService.removeBatchByIds(Arrays.asList(dataIds.split(","))));
    }

    @PutMapping("/valid/{dataId}")
    @ResponseBody
    public Result valid(@PathVariable Integer dataId) {
        return Result.decide(sysDictDataService.valid(dataId));
    }

    @PutMapping("/invalid/{dataId}")
    @ResponseBody
    public Result invalid(@PathVariable Integer dataId) {
        return Result.decide(sysDictDataService.invalid(dataId));
    }

    @GetMapping("/selectByCode")
    @ResponseBody
    public Result<List<SysDictData>> selectByCode(String typeCode) {
        List<SysDictData> list = sysDictDataService.selectByTypeCode(typeCode);
        return Result.success(list);
    }

    @GetMapping("/getDictItems/{dictCode}")
    @ResponseBody
    public Result getDictItems(@PathVariable String dictCode) {
        System.out.println(dictCode);
        return Result.success();
    }

}
